fix: Set default quality flags when saving in JPEG format#3034
fix: Set default quality flags when saving in JPEG format#3034Eideren merged 2 commits intostride3d:masterfrom
Conversation
* Save JPEGs with quality 90 and chroma subsampling 4:2:0 by default. * Reorganize the class to better allow format-specific tweaks.
Fine with me, changing this would be outside the scope of what this PR is trying to solve. |
|
Yeah, I know. I only say that because I've noticed other formats come out also different (different filesize than the original, for example). For those other formats it is not as important, because they are lossless. It's only JPEG that is lossy. Anyway, for a future PR, yes, I don't know if it merits complicating the logic to allow the user to set the quality or other parameters. What do other engines when saving images? |
|
Fairly shallow in terms of feature set, see https://docs.unity3d.com/6000.0/Documentation/ScriptReference/ImageConversion.EncodeToJPG.html |
|
I find interesting that Unity has specific methods only for specific formats they want to support (JPG, PNG, TGA, and EXR), allowing them to do specific configs and checks, and each of them is pretty well documented. |
PR Details
Context: After the recent migration from
System.Drawing.Common(using GDI+, Windows-specific) to FreeImage (cross-platform), theTestLoadAndSaveofTestImage(inStride.Graphics.Tests) started failing for me. However, it only fails when the source (reference image to compare against) and the destination formats are both JPEG.The failure seems to be caused by differences in the way FreeImage encodes JPEGs, the quality, chroma subsampling, and possibly other things. This makes the resulting JPEG be more compressed and with worse quality, and the test image comparison fails because the differences are greater than the allowed difference of 30.
Solution: This PR sets quality and chroma subsampling flags so FreeImage can save an image more in line with the default configuration GDI+ was producing before the migration, making the test pass again. The key part is in
SaveJpegFromMemory(), inStandardImageHelper.Desktop.cs.As an extra, I've reorganized
StandardImageHelperto be more readable and to be able to set custom format-specific flags easier in the future. Currently the files are being saved using FreeImage defaults, and custom flags are hardcoded. I've also improved the documentation and comments.Related Issue
No issue. Discussed briefly on Discord.
Types of changes
Checklist